/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.PASSUser;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.LocalBean;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author ryuju
 */
@Singleton
@LocalBean
public class UserManager {

    @EJB
    private PASSUserFacade userFacade;

    public PASSUser login(String id, String pw) {
        PASSUser pUser = userFacade.findByLoginName(id);
        Logger.getLogger("UserManager").log(Level.INFO, "debug : " + pUser.toString());
        if (null != pUser) {
            String p1 = pUser.getPasswd().toLowerCase().trim();
            String p2 = pw.toLowerCase().trim();
            Logger.getLogger("UserManager").log(Level.INFO, "debug : " + p1 + " ::::" + p2);
            if (p1.equalsIgnoreCase(p2)) {
                return pUser;
            }
        }
        return null;
    }
    public boolean register(String id, String pw, String userType, String asst_pw) {
        PASSUser pUser = userFacade.findByLoginName(id);
        
        if (null == pUser) {
            if(userType.contentEquals("Assistent")){
                if(asst_pw.equalsIgnoreCase("1234")){
                    pUser = new PASSUser(id, pw, userType);
                }
                else{
                    return false;
                }
            }
            else{
                pUser = new PASSUser(id, pw, userType);
            }
            userFacade.create(pUser);
            return true;
        }
        return false;
    }

    public String getUserType(PASSUser user) {
        return user.getUserType();
    }
    
    
}
